﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ESB.BAM.DAL
{
    /// <summary>
    /// This class implements a simple in-process cache for the profile mapping data.
    /// </summary>
    public class EsbTrackingProfilesDbCache
    {
        static EsbTrackingProfilesDbCache s_Cache = null;
        EsbTrackingProfile[] m_Profiles;
        ESBTrackingData[] m_Data;
        EsbTrackingOrchestrationData[] m_OrchData;
        /// <summary>
        /// Gets the cache.
        /// Initializes the cache if is not initialized yet.
        /// Thread safe.
        /// </summary>
        /// <returns>EsbTrackingProfilesDbCache object</returns>
        public static EsbTrackingProfilesDbCache GetCache()
        {
            var callToken = Microsoft.BizTalk.CAT.BestPractices.Framework.Instrumentation.TraceManager.TrackingComponent.TraceIn();
            if (s_Cache == null)
            {
                lock (typeof(EsbTrackingProfilesDbCache))
                {
                    if (s_Cache == null)
                    {
                        Microsoft.BizTalk.CAT.BestPractices.Framework.Instrumentation.TraceManager.TrackingComponent.TraceInfo("Building the tracking profiles cache.");
                        System.Configuration.ConnectionStringSettings ConString = Microsoft.Practices.ESB.Configuration.ConfigHelper.GetConnectionSettings("ESB.BAM.DAL.Properties.Settings.LiuEsbConnectionString");
                        EsbTrackingProfilesDbModelDataContext ctxt = new EsbTrackingProfilesDbModelDataContext(ConString.ConnectionString);
                        EsbTrackingProfilesDbCache l_Cache = new EsbTrackingProfilesDbCache();
                        l_Cache.m_Profiles = (from Profile in ctxt.EsbTrackingProfiles
                                              select Profile).ToArray();
                        l_Cache.m_Data = (from Data in ctxt.ESBTrackingDatas
                                          select Data).ToArray();
                        l_Cache.m_OrchData = (from Data in ctxt.EsbTrackingOrchestrationDatas
                                              select Data).ToArray();
                        s_Cache = l_Cache;
                    }
                }
            }
            Microsoft.BizTalk.CAT.BestPractices.Framework.Instrumentation.TraceManager.TrackingComponent.TraceOut(callToken, s_Cache);
            return s_Cache;
        }
        /// <summary>
        /// Gets the profile step.
        /// </summary>
        /// <param name="ItineraryName">Name of the itinerary.</param>
        /// <param name="ServiceName">Name of the service.</param>
        /// <returns>EsbTrackingProfile object</returns>
        public EsbTrackingProfile GetProfileStep(string ItineraryName, string ServiceName)
        {
            EsbTrackingProfile profile = null;
            var callToken = Microsoft.BizTalk.CAT.BestPractices.Framework.Instrumentation.TraceManager.TrackingComponent.TraceIn(ItineraryName, ServiceName);
            try
            {
                profile = (from p in m_Profiles
                           where (p.ItineraryName == ItineraryName && p.ServiceName == ServiceName)
                           select p).SingleOrDefault();
            }
            catch (Exception ex)
            {
                Microsoft.BizTalk.CAT.BestPractices.Framework.Instrumentation.TraceManager.TrackingComponent.TraceError(ex, true, callToken);
                throw;
            }
            Microsoft.BizTalk.CAT.BestPractices.Framework.Instrumentation.TraceManager.TrackingComponent.TraceOut(callToken, profile);
            return profile;
        }
        /// <summary>
        /// Gets the data.
        /// </summary>
        /// <param name="ServiceMilestoneId">The service milestone id.</param>
        /// <returns>the list of the ESBTrackingData</returns>
        public List<ESBTrackingData> GetData(long ServiceMilestoneId)
        {
            var callToken = Microsoft.BizTalk.CAT.BestPractices.Framework.Instrumentation.TraceManager.TrackingComponent.TraceIn(ServiceMilestoneId);
            List<ESBTrackingData> datalist = null;
            try
            {
                datalist = (from Data in m_Data
                            where Data.ServiceMilestoneId == ServiceMilestoneId
                            select Data).ToList();
            }
            catch (Exception ex)
            {
                Microsoft.BizTalk.CAT.BestPractices.Framework.Instrumentation.TraceManager.TrackingComponent.TraceError(ex, true, callToken);
                throw;
            }
            Microsoft.BizTalk.CAT.BestPractices.Framework.Instrumentation.TraceManager.TrackingComponent.TraceOut(callToken);
            return datalist;
        }

        /// <summary>
        /// Gets the orchestration data list.
        /// </summary>
        /// <param name="OrchestrationName">Name of the orchestration.</param>
        /// <param name="ShapeName">Name of the shape.</param>
        /// <returns>A list of EsbTrackingOrchestrationData</returns>
        public List<EsbTrackingOrchestrationData> GetOrchestrationDataList(string OrchestrationName, string ShapeName)
        {
            var callToken = Microsoft.BizTalk.CAT.BestPractices.Framework.Instrumentation.TraceManager.TrackingComponent.TraceIn(OrchestrationName, ShapeName);
            List<EsbTrackingOrchestrationData> lData = null;
            lData = (from Data in m_OrchData
                     where (Data.OrchestrationName == OrchestrationName && Data.ShapeName == ShapeName)
                     select Data).ToList();
            Microsoft.BizTalk.CAT.BestPractices.Framework.Instrumentation.TraceManager.TrackingComponent.TraceOut(callToken, lData.Count);
            return lData;
        }
    }
}
